package com.yanfan.maintain.service.impl;

import com.yanfan.attach.mapper.DeviceDetailMapper;
import com.yanfan.common.core.domain.entity.SysDictData;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.domain.ThingsModel;
import com.yanfan.iot.mapper.DeviceMapper;
import com.yanfan.iot.mapper.ThingsModelMapper;
import com.yanfan.maintain.domain.*;
import com.yanfan.maintain.mapper.ControlFormworkDeviceMapper;
import com.yanfan.maintain.mapper.ControlFormworkMapper;
import com.yanfan.maintain.mapper.ControlReversalMapper;
import com.yanfan.maintain.mapper.ControlRuleMapper;
import com.yanfan.maintain.service.IControlFormworkService;
import com.yanfan.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 头注释Service业务层处理
 *
 * @author kerwincui
 * @date 2023-03-13
 */
@Service
public class ControlFormworkServiceImpl implements IControlFormworkService {
    @Autowired
    private ControlFormworkMapper controlFormworkMapper;

    @Autowired
    private ControlFormworkDeviceMapper controlFormworkDeviceMapper;
    @Autowired
    private ControlReversalMapper controlReversalMapper;
    @Autowired
    private ControlRuleMapper controlRuleMapper;
    @Autowired
    private DeviceDetailMapper deviceDetailMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ThingsModelMapper thingsModelMapper;
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询头注释
     *
     * @param id 头注释主键
     * @return 头注释
     */
    @Override
    public ControlFormwork selectControlFormworkById(Long id) {
        return controlFormworkMapper.selectControlFormworkById(id);
    }

    /**
     * 查询头注释列表
     *
     * @param controlFormwork 头注释
     * @return 头注释
     */
    @Override
    public List<ControlFormwork> selectControlFormworkList(ControlFormwork controlFormwork) {
        return controlFormworkMapper.selectControlFormworkList(controlFormwork);
    }

    /**
     * 新增头注释
     *
     * @param controlFormwork 头注释
     * @return 结果
     */
    @Override
    public int insertControlFormwork(ControlFormwork controlFormwork) {
        controlFormwork.setCreateTime(DateUtils.getNowDate());
        controlFormwork.setUpdateTime(DateUtils.getNowDate());
        controlFormwork.setCreateId(SecurityUtils.getUserId());
        controlFormwork.setCreateName(SecurityUtils.getUsername());
        return controlFormworkMapper.insertControlFormwork(controlFormwork);
    }

    /**
     * 修改头注释
     *
     * @param controlFormwork 头注释
     * @return 结果
     */
    @Override
    public int updateControlFormwork(ControlFormwork controlFormwork) {
        controlFormwork.setUpdateTime(DateUtils.getNowDate());
        return controlFormworkMapper.updateControlFormwork(controlFormwork);
    }

    /**
     * 批量删除头注释
     *
     * @param ids 需要删除的头注释主键
     * @return 结果
     */
    @Override
    public int deleteControlFormworkByIds(Long[] ids) {
        return controlFormworkMapper.deleteControlFormworkByIds(ids);
    }

    /**
     * 删除头注释信息
     *
     * @param id 头注释主键
     * @return 结果
     */
    @Override
    public int deleteControlFormworkById(Long id) {
        return controlFormworkMapper.deleteControlFormworkById(id);
    }

    @Override
    public void addDevice(List<ControlRuleRequest> controlRuleRequestList) {
        // 添加时先删除所有数据
        Long formWorkId = controlRuleRequestList.get(0).getFormWorkId();

        controlFormworkDeviceMapper.deleteControlFormworkDeviceByFormWorkId(formWorkId);
        controlRuleMapper.deleteByFormWorkId(formWorkId);
        controlReversalMapper.deleteByFormWorkId(formWorkId);


        for (int i = 0; i < controlRuleRequestList.size(); i++) {
            ControlRuleRequest controlRuleRequest = controlRuleRequestList.get(i);
            // 添加设备
            Long deviceId = controlRuleRequest.getDeviceId();
            Long formworkId = controlRuleRequest.getFormWorkId();
            ControlFormworkDevice deviceEntity = new ControlFormworkDevice();

            deviceEntity.setDeviceId(deviceId);
            deviceEntity.setFormworkId(formworkId);
            deviceEntity.setDeviceName(controlRuleRequest.getDeviceName());
            deviceEntity.setCreateName(SecurityUtils.getUsername());
            deviceEntity.setCreateBy(SecurityUtils.getUserId() + "");
            deviceEntity.setActionText(controlRuleRequest.getActionText());
            deviceEntity.setRuleText(controlRuleRequest.getRuleText());
            deviceEntity.setExecModel(controlRuleRequest.getExecModel());
            deviceEntity.setSortNumber(i + 1);
            controlFormworkDeviceMapper.insertControlFormworkDevice(deviceEntity);
            // 添加规则
            List<ControlRule> rules = controlRuleRequest.getRules();
            for (ControlRule rule : rules) {
                rule.setDeviceId(deviceId);
                rule.setFormworkId(formworkId);
                controlRuleMapper.insertControlRule(rule);
            }
            // 添加反控
            List<ControlReversal> reversals = controlRuleRequest.getReversals();
            for (ControlReversal reversal : reversals) {
                reversal.setDeviceId(deviceId);
                reversal.setFormworkId(formworkId);
                controlReversalMapper.insertControlReversal(reversal);
            }
        }
    }

    @Override
    public List<Device> getDeviceList(String text) {
        return deviceDetailMapper.searchDevice(text);

    }

    @Override
    public List<ControlRuleRequest> getDeviceByFormworkId(Long formworkId) {
        List<ControlRuleRequest> requests = new ArrayList<>();

        // 查询设备
        List<ControlFormworkDevice> devices = controlFormworkDeviceMapper.findByFormworkId(formworkId);
        // 查询规则
        ControlRule controlRule = new ControlRule();
        controlRule.setFormworkId(formworkId);
        List<ControlRule> rules = controlRuleMapper.selectControlRuleList(controlRule);
        // 查询反控
        ControlReversal controlReversal = new ControlReversal();
        controlReversal.setFormworkId(formworkId);
        List<ControlReversal> reversals = controlReversalMapper.selectControlReversalList(controlReversal);
        for (ControlFormworkDevice device : devices) {
            ControlRuleRequest request = new ControlRuleRequest();
            request.setDeviceId(device.getDeviceId());
            request.setFormWorkId(formworkId);
            request.setDeviceName(device.getDeviceName());
            request.setActionText(device.getActionText());
            request.setRuleText(device.getRuleText());
            request.setReversals(reversals
                                         .stream()
                                         .filter(item -> item.getDeviceId().equals(device.getDeviceId()))
                                         .collect(Collectors.toList()));
            request.setRules(rules
                                     .stream()
                                     .filter(item -> item.getDeviceId().equals(device.getDeviceId()))
                                     .collect(Collectors.toList()));
            requests.add(request);
        }
        return requests;
    }

    @Override
    public List<Map> getParam(String category, Long deviceId) {
        List<Map> result = new ArrayList<>();

        if (category.equals("model")) {
            // 查询产品的模型
            Device device = deviceMapper.selectDeviceByDeviceId(deviceId);
            ThingsModel param = new ThingsModel();
            param.setProductId(device.getProductId());
            param.setLanguage(SecurityUtils.getLanguage());
            List<ThingsModel> thingsModels = thingsModelMapper.selectThingsModelList(param);
            for (ThingsModel thingsModel : thingsModels) {
                Map map = new HashMap();
                map.put("name", thingsModel.getModelName());
                map.put("value", thingsModel.getIdentifier());
                result.add(map);
            }

        } else if (category.equals("energy")) {
            Map map = new HashMap();
            map.put("name", "纯水");
            map.put("value", "water");
            Map map2 = new HashMap();
            map2.put("name", "软化水");
            map2.put("value", "softWater");
            Map map3 = new HashMap();
            map3.put("name", "压缩空气");
            map3.put("value", "comAir");
            Map map4 = new HashMap();
            map4.put("name", "燃气");
            map4.put("value", "gas");
            Map map5 = new HashMap();
            map5.put("name", "电压");
            map5.put("value", "voltage");
            result.add(map);
            result.add(map2);
            result.add(map3);
            result.add(map4);
            result.add(map5);
        } else if (category.equals("time")) {
            Map map = new HashMap();
            map.put("name", "秒");
            map.put("value", "second");
            Map map2 = new HashMap();
            map2.put("name", "分");
            map2.put("value", "minute");
            Map map3 = new HashMap();
            map3.put("name", "时");
            map3.put("value", "hour");
            Map map4 = new HashMap();
            map4.put("name", "日");
            map4.put("value", "day");
            Map map5 = new HashMap();
            map5.put("name", "月");
            map5.put("value", "month");
            result.add(map);
            result.add(map2);
            result.add(map3);
            result.add(map4);
            result.add(map5);
        } else if (category.equals("scale")) {
            Map map = new HashMap();
            map.put("name", "纯水");
            map.put("value", "water");
            Map map2 = new HashMap();
            map2.put("name", "软化水");
            map2.put("value", "softWater");
            Map map3 = new HashMap();
            map3.put("name", "压缩空气");
            map3.put("value", "comAir");
            Map map4 = new HashMap();
            map4.put("name", "燃气");
            map4.put("value", "gas");
            Map map5 = new HashMap();
            map5.put("name", "电压");
            map5.put("value", "voltage");
            result.add(map);
            result.add(map2);
            result.add(map3);
            result.add(map4);
            result.add(map5);
        } else if (category.equals("outParam")) {

        }
        return result;

    }

    @Override
    public List<Map> getCategory() {
        List<Map> result = new ArrayList<>();
        Map map = new HashMap();
        map.put("name", "物理模型");
        map.put("value", "model");

        Map map1 = new HashMap();
        map1.put("name", "能源");
        map1.put("value", "energy");

        Map map2 = new HashMap();
        map2.put("name", "时间");
        map2.put("value", "time");

        Map map3 = new HashMap();
        map3.put("name", "计算比值");
        map3.put("value", "scale");

        Map map4 = new HashMap();
        map4.put("name", "外部参数");
        map4.put("value", "outParam");

        result.add(map);
        result.add(map1);
        result.add(map2);
        result.add(map3);
        result.add(map4);
        return result;
    }

    @Override
    public List<Map> getCondition() {
        List<Map> result = new ArrayList<>();
        Map map = new HashMap();
        map.put("name", "大于");
        map.put("value", ">");

        Map map1 = new HashMap();
        map1.put("name", "小于");
        map1.put("value", "<");

        Map map2 = new HashMap();
        map2.put("name", "等于");
        map2.put("value", "=");

        Map map3 = new HashMap();
        map3.put("name", "大于等于");
        map3.put("value", ">=");

        Map map4 = new HashMap();
        map4.put("name", "小于等于");
        map4.put("value", "<=>");

        Map map5 = new HashMap();
        map5.put("name", "包含");
        map5.put("value", "contain");

        Map map6 = new HashMap();
        map6.put("name", "不包含");
        map6.put("value", "notIncluded");

        result.add(map);
        result.add(map1);
        result.add(map2);
        result.add(map3);
        result.add(map4);
        result.add(map5);
        result.add(map6);

        return result;
    }

    @Override
    public List<Map> getIotDataType() {
        List<Map> result = new ArrayList<>();
        List<SysDictData> data = dictTypeService.selectDictDataByType("iot_data_type");
        for (SysDictData datum : data) {
            Map map6 = new HashMap();
            map6.put("name", datum.getDictLabel());
            map6.put("value", datum.getDictValue());
            result.add(map6);
        }
        return result;
    }

    @Override
    public List<Map> getAttr() {
        List<Map> result = new ArrayList<>();
        Map map = new HashMap();
        map.put("name", "属性");
        map.put("value", "property");

        Map map1 = new HashMap();
        map1.put("name", "功能");
        map1.put("value", "function");
        result.add(map);
        result.add(map1);

        return result;
    }
}
